﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using NetBay.Core.Tools;
#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert User database row to User object
    /// </summary>
    public class UserTranslator
    {
        /// <summary>
        /// Translate a bunch of User DataReader to Group object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of User Object</returns>
        public static UserRepository TranslateFind(IDataReader P_oDbDataReader)
        {
            var L_oUser = new UserRepository();
            L_oUser.IsValid = false;

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        L_oUser.FirstName = P_oDbDataReader["RUSER_NOM"].ToString();
                        L_oUser.LastName = P_oDbDataReader["RUSER_PRENOM"].ToString();
                        L_oUser.UserId = int.Parse(P_oDbDataReader["RUSER_NO"].ToString());
                        L_oUser.Email = P_oDbDataReader["RUSER_EMAIL"].ToString();
                        L_oUser.Login = P_oDbDataReader["RUSER_LOGIN"].ToString();
                        L_oUser.Password = P_oDbDataReader["RUSER_PASSWD"].ToString();
                        L_oUser.IsValid = true;
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("EXCEPTION -> " + Environment.NewLine + ex);
            }
            
            return L_oUser;
        }

        /// <summary>
        /// Translate a bunch of User DataReader to Group object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of User Object</returns>
        public static List<User> TranslateUsers(IDataReader P_oDbDataReader)
        {
            var l_lstUsers = new List<User>();

            try
            {
                using (P_oDbDataReader)
                {
                    // Read all rows
                    while (P_oDbDataReader.Read())
                    {
                        TranslateOne(P_oDbDataReader, l_lstUsers);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstUsers;
        }

        /// <summary>
        /// Translates the user.
        /// </summary>
        /// <param name="p_oIDataReader">The data reader.</param>
        /// <returns></returns>
        public static User TranslateUser(IDataReader p_oIDataReader)
        {
            var l_oUser = new User();

            try
            {
                using (p_oIDataReader)
                {
                    p_oIDataReader.Read();
                    l_oUser.FirstName = p_oIDataReader["RUSER_NOM"].ToString();
                    l_oUser.LastName = p_oIDataReader["RUSER_PRENOM"].ToString();
                    l_oUser.UserId = int.Parse(p_oIDataReader["RUSER_NO"].ToString());
                    l_oUser.Email = p_oIDataReader["RUSER_EMAIL"].ToString();
                    l_oUser.Login = p_oIDataReader["RUSER_LOGIN"].ToString();
                    l_oUser.Password = p_oIDataReader["RUSER_PASSWD"].ToString();
                    l_oUser.IsValid = true;
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_oUser;
        }

        private static void TranslateOne(IDataReader p_oIDataReader, List<User> l_lstUsers)
        {
            var l_oUser = new User();

            try
            {
                l_oUser.FirstName = p_oIDataReader["RUSER_NOM"].ToString();
                l_oUser.LastName = p_oIDataReader["RUSER_PRENOM"].ToString();
                l_oUser.UserId = int.Parse(p_oIDataReader["RUSER_NO"].ToString());
                l_oUser.Email = p_oIDataReader["RUSER_EMAIL"].ToString();
                l_oUser.Login = p_oIDataReader["RUSER_LOGIN"].ToString();
                l_oUser.Password = p_oIDataReader["RUSER_PASSWD"].ToString();
                l_oUser.IsValid = true;
                // All members are readed, so save this document to collection
                l_lstUsers.Add(l_oUser);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }

        /// <summary>
        /// Translate a bunch of EmailUser DataReader to a list
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of EmailUser</returns>
        public static List<string> TranslateEmailUsers(IDataReader P_oDbDataReader)
        {
            var l_oEmailUsers = new List<string>();

            try
            {
                using (P_oDbDataReader)
                {
                    // Anything to read?
                    while (P_oDbDataReader.Read())
                    {
                        TranslateEmailUserOne(P_oDbDataReader, l_oEmailUsers);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_oEmailUsers;
        }


        private static void TranslateEmailUserOne(IDataReader p_oIDataReader, List<string> p_oEmailUsers)
        {
            try
            {
                var l_szEmailUser = string.Empty;
                l_szEmailUser = p_oIDataReader["ruser_email"].ToString();
                if (string.IsNullOrWhiteSpace(l_szEmailUser) == false)
                {
                    p_oEmailUsers.Add(l_szEmailUser);
                }

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }


        public static string SerializeIndex(Dictionary<long, string> p_sIndexes)
        {

            string l_sMemStr = null;
            if (p_sIndexes != null)
            {
                l_sMemStr = FileSerializer.SerializeBinaryIndex(p_sIndexes);

            }


            return l_sMemStr;
        }


        public static Dictionary<long, string> DeSerializeIndex(string val)
        {

            return FileSerializer.DeSerializeBinaryIndex<Dictionary<long, string>>(val);
        }

        /// <summary>
        /// Updates the user password.
        /// </summary>
        /// <param name="p_oIDataReader">The p_o I data reader.</param>
        /// <returns></returns>
        public static bool UpdateUserPassword(IDataReader p_oIDataReader)
        {
            bool success = false;

            try
            {
                using (p_oIDataReader)
                {
                    while (p_oIDataReader.Read())
                    {
                        success = Convert.ToBoolean(Int32.Parse(p_oIDataReader["PASSWORD_CHANGED"].ToString()));
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return success;
        }
    }
}
